'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"

GITHUB_USER_PW = "qwer1234"

# The criteria date that a developer gets involved in a certain project 
REPO_COMMIT_DURATION_DAYS = 90 

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID, password=GITHUB_USER_PW)
print str(gh.ratelimit_remaining) + ' request remaining'

# Github Bootstrap Repository information
GITHUB_USER_NAME = 'twitter'
GITHUB_REPOSITORY_NAME = 'bootstrap'

# Bootstrap collaborators information file
COLLABORATORS_FILE = 'bootstrap_collaborators.txt'

# Bootstrap migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'bootstrap_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github Bootstrap project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        if item == 'olix0r':
            print str(gh.ratelimit_remaining) + ' request remaining'
            time.sleep(60 * 60)

#        item = 'WamBamBoozle'
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
#        print len(repo_list)
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('Bootstrap Developer, Migrants(0)/Debutants(1)//No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

# It's a time to sleep for 1 hour
time.sleep(60 * 60)

###########################################################################
# Github Homebrew Repository information
GITHUB_USER_NAME = 'mxcl'
GITHUB_REPOSITORY_NAME = 'homebrew'

# Homebrew collaborators information file
COLLABORATORS_FILE = 'homebrew_collaborators.txt'

# Homebrew migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'homebrew_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github Homebrew project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('Homebrew Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

###########################################################################
# Github Rails Repository information
GITHUB_USER_NAME = 'rails'
GITHUB_REPOSITORY_NAME = 'rails'

# Rails collaborators information file
COLLABORATORS_FILE = 'rails_collaborators.txt'

# Rails migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'rails_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github Rails project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('Rails Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'Rails Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

# It's a time to sleep for 1 hour
time.sleep(60 * 60)

###########################################################################
# Github html5 boilerplate Repository information
GITHUB_USER_NAME = 'h5bp'
GITHUB_REPOSITORY_NAME = 'html5-boilerplate'

# html5 boilerplate collaborators information file
COLLABORATORS_FILE = 'html5_boilerplate_collaborators.txt'

# html5 boilerplate migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'html5_boilerplate_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github html5 boilerplate project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('html5 boilerplate Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'html5 boilerplate Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

###########################################################################
# Github oh-my-zsh Repository information
GITHUB_USER_NAME = 'robbyrussell'
GITHUB_REPOSITORY_NAME = 'oh-my-zsh'

# oh-my-zsh collaborators information file
COLLABORATORS_FILE = 'oh_my_zsh_collaborators.txt'

# oh-my-zsh migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'oh_my_zsh_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github oh-my-zsh project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('oh-my-zsh Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'oh-my-zsh Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'
time.sleep(60 * 60)

###########################################################################
# Github node Repository information
GITHUB_USER_NAME = 'joyent'
GITHUB_REPOSITORY_NAME = 'node'

# Node collaborators information file
COLLABORATORS_FILE = 'node_collaborators.txt'

# Node migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'node_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github node project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('node Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'node Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

###########################################################################
# Github jquery Repository information
GITHUB_USER_NAME = 'jquery'
GITHUB_REPOSITORY_NAME = 'jquery'

# jquery collaborators information file
COLLABORATORS_FILE = 'jquery_collaborators.txt'

# jquery migrants_debutants information file
MIGRANTS_DEBUTANTS_FILE = 'jquery_migrants_debutants.txt'

project_collaborator = []
migrants_debutants_dict = {}

try:
    # Github jquery project collaborators list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborator.append(user_login)

    f_collaborators.close()
    
    for item in project_collaborator:
        print 'login id: ' +  item
        repo_list = list(gh.iter_repos(login=item, type='all', sort='updated'))
        if not repo_list:
            print 'no repository list information'
            continue

        dev_first_commitdate = datetime.datetime.now()
        dev_first_commit_repo = ''
        dev_first_commit = ''
        for repo in repo_list:
#            print repo.html_url, repo.is_collaborator(item)
            try:
                repo_commit_list = list(repo.iter_commits(author=item, number = -1))
                if not repo_commit_list:
#                    print 'no commit information'
                    continue

                repo_firstcommit = repo_commit_list[len(repo_commit_list) - 1]             
                # Checking what if there is no commit history
                repo_first_commit_date = repo_firstcommit.commit.author['date']
                first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
#                print repo.html_url, 'First date: ' + str(first_date)
                if first_date < dev_first_commitdate:
                    dev_first_commitdate = first_date
                    dev_first_commit_repo = repo
                    dev_first_commit = repo_firstcommit

            except Exception as e: 
                print 'The 1st Exception' + str(e.args[0])

        # The part of evaluating which a developer is migrants or debutants or no committer
        if( dev_first_commit_repo == ''):
            migrants_debutants_dict[item] = 2
#        print dev_first_commit_repo.html_url, dev_first_commitdate
        elif( dev_first_commit_repo.html_url.endswith(GITHUB_REPOSITORY_NAME)):
#            print 'Debutants(1)'
            migrants_debutants_dict[item] = 1
        else:
#            print 'Migrants(0)'
            migrants_debutants_dict[item] = 0
#        break

    f_migrants_debutants = open(MIGRANTS_DEBUTANTS_FILE, 'w')
    print 'Printing dictionary'
    f_migrants_debutants.write('jquery Developer, Migrants(0)/Debutants(1)/No Committer(2) \n')
    for login_id, projects in migrants_debutants_dict.items():
        print 'jquery Developer: ' + str(login_id) + ", Migrants(0)/Debutants(1)/No Committer(2): " + str(projects)
        f_migrants_debutants.write(str(login_id) + ', ' + str(projects) + '\n')
    
    f_migrants_debutants.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

###########################################################################
print str(gh.ratelimit_remaining) + ' request remaining'

print
print 'Finally the end'
